package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.Viewpoint;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.service.IViewpointService;
import cn.wolfcode.luowowo.cache.key.RedisKey;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.service.ITravelStatsCacheService;
import cn.wolfcode.luowowo.cache.service.IViewpointStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.cache.vo.TravelStats;
import cn.wolfcode.luowowo.cache.vo.ViewpointStats;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

//统计数据初始化监听器
//接口中泛型ContextRefreshedEvent:表示spring创建结束的事件
@Component
public class StatsDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IViewpointService viewpointService;
    @Reference
    private IViewpointStatsCacheService viewpointStatsCacheService;
    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelStatsCacheService travelStatsCacheService;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //完成数据的统计,将mysql中的数据导入redis
       // System.err.println(strategyDetailService);
        //1.查询所有的文章
        List<StrategyDetail> details = strategyDetailService.listAll();
        //2.迭代list对象
        for (StrategyDetail detail : details) {
            //判断vo对象在redis中是否已经存在,如皋不存在就存入redis中
            if (!strategyStatsCacheService.hasStatsKey(detail.getId())){
                //3封装成vo对象
                StrategyStats stats = new StrategyStats();
                stats.setStrategyId(detail.getId());
                stats.setViewnum(detail.getViewnum());
                stats.setFavornum(detail.getFavornum());
                stats.setReplynum(detail.getReplynum());
                stats.setSharenum(detail.getSharenum());
                stats.setThumbsupnum(detail.getThumbsupnum());
                Destination dest = detail.getDest();
                stats.setDestId(dest.getId());
                stats.setDestName(dest.getName());
                stats.setTitle(detail.getTitle());
                //保存到redis中
                strategyStatsCacheService.setStrategyStats(stats);

                //飙升排行榜的数据初始化
                //num:分值
                if (strategyStatsCacheService.exist(RedisKey.STRATEGY_STATS_UP_SORT,detail.getId())){
                    int num = detail.getViewnum() + detail.getReplynum();
                    strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_UP_SORT,num,detail.getId());
                }
                //热门排行榜的数据初始化
                //num:分值
                if (strategyStatsCacheService.exist(RedisKey.STRATEGY_STATS_HOT_SORT,detail.getId())){
                    int num = detail.getFavornum() + detail.getThumbsupnum();
                    strategyStatsCacheService.addScore(RedisKey.STRATEGY_STATS_HOT_SORT,num,detail.getId());
                }
            }
            //将游记中sql中的数据导入到redis中
            //1.查询所有的游记
            List<Travel> travels = travelService.listAll();
            for (Travel travel : travels) {
                //判断vo对象在redis中是否存在,如果奴存在就存入redis中
                if(!travelStatsCacheService.hasStatsKey(travel.getId())){
                    //封装成vo对象
                    TravelStats travelStats = new TravelStats();
                    travelStats.setTravelId(travel.getId());
                    travelStats.setViewnum(travel.getViewnum());
                    travelStats.setFavornum(travel.getFavornum());
                    travelStats.setReplynum(travel.getReplynum());
                    travelStats.setSharenum(travel.getSharenum());
                    travelStats.setThumbsupnum(travel.getThumbsupnum());
                    Destination dest = travel.getDest();
                    if (dest != null) {
                        travelStats.setDestId(dest.getId());
                        travelStats.setDestName(dest.getName());
                    }
                    travelStats.setTitle(travel.getTitle());
                    //保存到redis中
                    travelStatsCacheService.setTravelStats(travelStats);
                }
            }

        }
        //景点热数据初始化
        List<Viewpoint> vps = viewpointService.list();
        if(vps != null && vps.size() > 0){
            for (Viewpoint vp : vps) {
                if (!viewpointStatsCacheService.hasStatsKey(vp.getId())){
                    ViewpointStats stats = new ViewpointStats();
                    stats.setViewpointId(vp.getId());
                    stats.setFavornum(vp.getFavorNum());
                    stats.setReplynum(vp.getReplyNum());
                    stats.setCamenum(vp.getCameNum());
                    viewpointStatsCacheService.setViewpointStats(stats);
                }
            }
        }
    }
}
